package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.system.SystemModuleInfoBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemModuleInfoDao;
import com.cw.jeeyt.dao.model.system.SystemModuleInfoDo;
import com.cw.jeeyt.service.converter.system.SystemModuleInfoConverter;
import com.cw.jeeyt.service.intf.system.SystemModuleInfoService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 模块信息业务实现类
 *
 * @author chenw
* @version 1.0.0
 */
@Service
public class SystemModuleInfoServiceImpl implements SystemModuleInfoService {

    private static final Logger logger = LoggerFactory.getLogger(SystemModuleInfoServiceImpl.class);
    @Autowired
    private SystemModuleInfoDao systemModuleInfoDao;

    /**
     * 新增SystemModuleInfo
     *
     * @param systemModuleInfoBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSystemModuleInfo(SystemModuleInfoBo systemModuleInfoBo) throws Exception {

        if (systemModuleInfoBo == null) {
            logger.error("模块信息为空");
            return;
        }
        SystemModuleInfoDo systemModuleInfo = SystemModuleInfoConverter.bo2Do(systemModuleInfoBo);
        systemModuleInfo.setId(SequenceTool.nextId());
        systemModuleInfo.setCreateBy(SessionUtil.getSessionUserId());
        systemModuleInfo.setCreateDate(new Date());
        systemModuleInfo.setUpdateBy(SessionUtil.getSessionUserId());
        systemModuleInfo.setUpdateDate(new Date());
        systemModuleInfoDao.insertSystemModuleInfo(systemModuleInfo);
    }

    /**
     * 更新SystemModuleInfo信息
     *
     * @param systemModuleInfoBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSystemModuleInfo(SystemModuleInfoBo systemModuleInfoBo) throws Exception {

        if (systemModuleInfoBo == null) {
            logger.error("模块信息为空");
            return;
        }
        SystemModuleInfoDo systemModuleInfo = SystemModuleInfoConverter.bo2Do(systemModuleInfoBo);
        systemModuleInfo.setUpdateBy(SessionUtil.getSessionUserId());
        systemModuleInfo.setUpdateDate(new Date());
        systemModuleInfoDao.updateSystemModuleInfo(systemModuleInfo);
    }

    /**
     * 删除SystemModuleInfo
     *
     * @param systemModuleInfoBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSystemModuleInfo(SystemModuleInfoBo systemModuleInfoBo) throws Exception {
        if (systemModuleInfoBo == null) {
            logger.error("模块信息为空");
            return;
        }
        systemModuleInfoDao.deleteSystemModuleInfo(SystemModuleInfoConverter.bo2Do(systemModuleInfoBo));

    }

    /**
     * 分页查询SystemModuleInfo
     *
     * @param systemModuleInfoBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemModuleInfoBo> queryByPage(SystemModuleInfoBo systemModuleInfoBo, Integer startNum,
        Integer pageSize) throws Exception {
        PageInfo<SystemModuleInfoDo> pageInfo = systemModuleInfoDao.queryByPage(
            SystemModuleInfoConverter.bo2Do(systemModuleInfoBo), startNum, pageSize);
        return new PageInfo<>(pageInfo, SystemModuleInfoConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询SystemModuleInfo信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemModuleInfoBo getSystemModuleInfoById(String id) throws Exception {
        return SystemModuleInfoConverter.do2Bo(systemModuleInfoDao.getSystemModuleInfoById(id));
    }

    /**
     * 根据项目id查询模块信息
     */
    @Override
    public List<SystemModuleInfoBo> querySystemModuleInfoListByProjectId(String projectId) throws Exception {

        return SystemModuleInfoConverter.doList2BoList(
            systemModuleInfoDao.querySystemModuleInfoListByProjectId(projectId));
    }
}
